package com.minimalist.lindi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Assert;
import com.minimalist.common.config.exception.BusinessException;
import com.minimalist.common.entity.lindi.po.LindiSubsidyApplicationTownPayment;
import com.minimalist.common.entity.lindi.vo.subsidyapplicationtownpayment.LindiSubsidyApplicationTownPaymentVO;
import com.minimalist.common.utils.UnqIdUtil;
import com.minimalist.lindi.mapper.LindiSubsidyApplicationTownPaymentMapper;
import com.minimalist.lindi.service.LindiSubsidyApplicationTownPaymentService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 林地指标平台_镇街支付明细表 服务层实现。
 *
 * @author 11987
 * @since 2025-07-06
 */
@Service
public class LindiSubsidyApplicationTownPaymentServiceImpl extends ServiceImpl<LindiSubsidyApplicationTownPaymentMapper, LindiSubsidyApplicationTownPayment> implements LindiSubsidyApplicationTownPaymentService{

    @Autowired
    private LindiSubsidyApplicationTownPaymentMapper mapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSubsidyApplicationTownPayment(LindiSubsidyApplicationTownPaymentVO subsidyApplicationTownPaymentVO){
        LindiSubsidyApplicationTownPayment townPayment = BeanUtil.copyProperties(
                subsidyApplicationTownPaymentVO, LindiSubsidyApplicationTownPayment.class);
        townPayment.setSubsidyApplicationTownPaymentId(UnqIdUtil.uniqueId());
        int insert = mapper.insert(townPayment, true);
        Assert.isTrue(insert == 1, () -> new BusinessException(
                "添加镇街支付明细失败"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSubsidyApplicationTownPaymentBySubsidyApplicationTownPaymentId(Long subsidyApplicationTownPaymentId){
        LindiSubsidyApplicationTownPayment townPayment = mapper
                .selectSubsidyApplicationTownPaymentBySubsidyApplicationTownPaymentId(subsidyApplicationTownPaymentId);
        Assert.notNull(townPayment, () -> new BusinessException(
                "镇街支付明细不存在"));
        int delete = mapper.deleteSubsidyApplicationTownPaymentBySubsidyApplicationTownPaymentId(subsidyApplicationTownPaymentId);
        Assert.isTrue(delete == 1, () -> new BusinessException(
                "删除镇街支付明细失败"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSubsidyApplicationTownPaymentBySubsidyApplicationTownPaymentId(LindiSubsidyApplicationTownPaymentVO subsidyApplicationTownPaymentVO){
        LindiSubsidyApplicationTownPayment townPayment = mapper
                .selectSubsidyApplicationTownPaymentBySubsidyApplicationTownPaymentId(
                        subsidyApplicationTownPaymentVO.getSubsidyApplicationTownPaymentId());
        Assert.notNull(townPayment, () -> new BusinessException(
                "镇街支付明细不存在"));
        BeanUtil.copyProperties(subsidyApplicationTownPaymentVO, townPayment,
                CopyOptions.create().ignoreNullValue());
        int update = mapper.update(townPayment);
        Assert.isTrue(update == 1, () -> new BusinessException(
                "修改镇街支付明细失败"));
    }

    @Override
    public LindiSubsidyApplicationTownPaymentVO getSubsidyApplicationTownPaymentBySubsidyApplicationTownPaymentId(Long subsidyApplicationTownPaymentId){
        LindiSubsidyApplicationTownPayment townPayment = mapper
                .selectSubsidyApplicationTownPaymentBySubsidyApplicationTownPaymentId(subsidyApplicationTownPaymentId);
        Assert.notNull(townPayment, () -> new BusinessException(
                "镇街支付明细不存在"));
        return BeanUtil.copyProperties(townPayment, LindiSubsidyApplicationTownPaymentVO.class);
    }

    @Override
    public List<LindiSubsidyApplicationTownPaymentVO> getSubsidyApplicationTownPaymentListBySubsidyApplicationPayNoticeId(Long subsidyApplicationPayNoticeId){
        List<LindiSubsidyApplicationTownPayment> townPaymentList = mapper
                .selectTownPaymentListBySubsidyApplicationPayNoticeId(subsidyApplicationPayNoticeId);
        return BeanUtil.copyToList(townPaymentList, LindiSubsidyApplicationTownPaymentVO.class);
    }

    @Override
    public void batchAddSubsidyApplicationTownPayment(List<LindiSubsidyApplicationTownPaymentVO> townPaymentList) {
        if (townPaymentList != null && !townPaymentList.isEmpty()) {
            for (LindiSubsidyApplicationTownPaymentVO townPaymentVO : townPaymentList) {
                LindiSubsidyApplicationTownPayment townPayment = BeanUtil.copyProperties(townPaymentVO, LindiSubsidyApplicationTownPayment.class);
                townPayment.setSubsidyApplicationTownPaymentId(UnqIdUtil.uniqueId());
                mapper.insert(townPayment, true);
            }
        }
    }

    @Override
    public void deleteSubsidyApplicationTownPaymentBySubsidyApplicationPayNoticeId(Long subsidyApplicationPayNoticeId) {
        mapper.deleteBySubsidyApplicationPayNoticeId(subsidyApplicationPayNoticeId);
    }

    @Override
    public List<LindiSubsidyApplicationTownPaymentVO> getSubsidyApplicationTownPaymentListBySubsidyApplicationId(Long subsidyApplicationId) {
        List<LindiSubsidyApplicationTownPayment> townPaymentList = mapper
                .selectTownPaymentListBySubsidyApplicationId(subsidyApplicationId);
        return BeanUtil.copyToList(townPaymentList, LindiSubsidyApplicationTownPaymentVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSubsidyApplicationTownPaymentBySubsidyApplicationId(Long subsidyApplicationId) {
        mapper.deleteBySubsidyApplicationId(subsidyApplicationId);
    }
}
